package com.hngates.project.controller;

import java.util.List;

import com.hngates.common.utils.ShiroUtils;
import com.hngates.project.domain.ConstructionUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hngates.common.annotation.Log;
import com.hngates.common.enums.BusinessType;
import com.hngates.project.domain.ConstructionContract;
import com.hngates.project.service.IConstructionContractService;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.utils.poi.ExcelUtil;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 施工合同Controller
 * 
 * @author ruoyi
 * @date 2022-06-22
 */
@Controller
@RequestMapping("/project/constructionContract")
public class ConstructionContractController extends BaseController
{
    private String prefix = "project/constructionContract";

    @Autowired
    private IConstructionContractService constructionContractService;

    @RequiresPermissions("project:constructionContract:view")
    @GetMapping()
    public String constructionContract()
    {
        return prefix + "/constructionContract";
    }

    /**
     * 查询施工合同列表
     */
    @RequiresPermissions("project:constructionContract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConstructionContract constructionContract)
    {
        startPage();
        List<ConstructionContract> list = constructionContractService.selectConstructionContractList(constructionContract);
        return getDataTable(list);
    }

    /**
     * 导出施工合同列表
     */
    @RequiresPermissions("project:constructionContract:export")
    @Log(title = "施工合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConstructionContract constructionContract)
    {
        List<ConstructionContract> list = constructionContractService.selectConstructionContractList(constructionContract);
        ExcelUtil<ConstructionContract> util = new ExcelUtil<ConstructionContract>(ConstructionContract.class);
        return util.exportExcel(list, "施工合同数据");
    }

    /**
     * 下载模板
     *
     * @return
     */
    @RequiresPermissions("project:constructionContract:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ConstructionContract> util = new ExcelUtil<>(ConstructionContract.class);
        return util.importTemplateExcel("施工合同数据");
    }

    /**
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */

    @RequiresPermissions("project:constructionContract:import")
    @PostMapping("/importConstructionContract")
    @ResponseBody
    public AjaxResult importConstructionContract(MultipartFile file, boolean updateSupport)throws Exception{
        ExcelUtil<ConstructionContract> util = new ExcelUtil<>(ConstructionContract.class);
        List<ConstructionContract> constructionContractList = util.importExcel(file.getInputStream(), 1);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = constructionContractService.importConstructionContract(constructionContractList, updateSupport, operName);
        return AjaxResult.success(message);

    }


    /**
     * 新增施工合同
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存施工合同
     */
    @RequiresPermissions("project:constructionContract:add")
    @Log(title = "施工合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ConstructionContract constructionContract)
    {
        return toAjax(constructionContractService.insertConstructionContract(constructionContract));
    }

    /**
     * 修改施工合同
     */
    @RequiresPermissions("project:constructionContract:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ConstructionContract constructionContract = constructionContractService.selectConstructionContractById(id);
        mmap.put("constructionContract", constructionContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工合同
     */
    @RequiresPermissions("project:constructionContract:edit")
    @Log(title = "施工合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConstructionContract constructionContract)
    {
        return toAjax(constructionContractService.updateConstructionContract(constructionContract));
    }

    /**
     * 删除施工合同
     */
    @RequiresPermissions("project:constructionContract:remove")
    @Log(title = "施工合同", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(constructionContractService.deleteConstructionContractByIds(ids));
    }
}
